package cn.flying.cloud.task.core.scheduler;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import cn.flying.cloud.task.core.context.JobHandlerCache;
import cn.flying.cloud.task.core.exception.TaskException;
import cn.flying.cloud.task.core.handler.AbstractJobHandler;
import cn.flying.cloud.task.core.model.JobContext;

/**
 * 调度定时任务
 *
 * @author: admin
 * @date: 2024年01月29日 23:28
 * @version: 1.0
 */
@Component
public class SchedQuartzJob {

    @Resource
    private JobHandlerCache jobHandlerCache;

    /**
     * 执行任务
     *
     * @param context
     */
    @Async
    public void run(JobContext context) {
        AbstractJobHandler jobHandler = jobHandlerCache.getJobHandlerCache().get(context.getJobName());

        if (jobHandler == null) {
            throw new TaskException("执行的任务不存在！");
        }
        // 调用 Bean 的 executeJob 方法
        jobHandler.executeJob(context);
    }
}
